﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using System.Xml.Linq;
using MyAlbum.Utility;


namespace MyAlbum.Data.Xml
{
    public class SupportRepository : XmlRepository, IRepository<MyAlbum.Model.Support>
    {

        private static object locker = "locker";
        public SupportRepository(string rootPath)
            : base(rootPath, "Supports")
        {
        }
        #region IRepository<Section> Members

        public List<MyAlbum.Model.Support> GetItems()
        {
            return this.Document.Descendants("Support").Select(s => new MyAlbum.Model.Support()
            {
                Id = s.Attribute("Id").Value,
                Name = s.Element("Name").Value.Trim(),
                Body = s.Element("Body").Value,
                TimeStamp = (DateTime)s.Element("TimeStamp"),
                IsActive = (bool)s.Element("IsActive"),
                Email = s.Element("Email").Value.Trim(),
                Subject = s.Element("Subject").Value.Trim()
            }).ToList();
        }

        public void AddItem(MyAlbum.Model.Support item)
        {
            Assert.IsNotNull(item);
            lock (locker)
            {
                item.Id = Guid.NewGuid().ToString();
                XElement a = new XElement("Support",
                    new XAttribute("Id", item.Id),
                    new XElement("IsActive", item.IsActive),
                    new XElement("TimeStamp", item.TimeStamp),
                    new XElement("Email", item.Email),
                    new XElement("Name", item.Name),
                    new XElement("Subject", item.Subject),
                    new XElement("Body", item.Body));
                Document.Element("Supports").Add(a);
                this.Save();
            }
        }

        public void DeleteItem(MyAlbum.Model.Support item)
        {
            Assert.IsNotNull(item);
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Support") where (e.Attribute("Id").Value) == item.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Remove();
                    this.Save();
                }
            }
        }

        public void UpdateItem(MyAlbum.Model.Support item)
        {
            Assert.IsNotNull(item);
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Support") where (e.Attribute("Id").Value) == item.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Element("Email").ReplaceWith(new XElement("Email", item.Email));
                    saved.Element("Body").ReplaceWith(new XElement("Body", item.Body));
                    saved.Element("Name").ReplaceWith(new XElement("Name", item.Name));
                    saved.Element("IsActive").ReplaceWith(new XElement("IsActive", item.IsActive));
                    saved.Element("Subject").ReplaceWith(new XElement("Subject", item.Subject));
                    this.Save();
                }
            }
        }

        public MyAlbum.Model.Support GetItemById(string id)
        {
            return this.Document.Descendants("Support").Where(s => s.Attribute("Id").Value == id).Select(s => new MyAlbum.Model.Support()
             {
                 Id = s.Attribute("Id").Value,
                 Name = s.Element("Name").Value.Trim(),
                 Body = s.Element("Body").Value,
                 TimeStamp = (DateTime)s.Element("TimeStamp"),
                 IsActive = (bool)s.Element("IsActive"),
                 Email = s.Element("Email").Value.Trim(),
                 Subject = s.Element("Subject").Value.Trim()
             }).SingleOrDefault();
        }

        #endregion
    }
}
